package worker.control;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import worker.model.service.ProcessService;
import worker.model.vo.ListVO;
import worker.model.vo.MemberVO;
import worker.model.vo.RealTimeInfoVO;
import worker.model.vo.WorkOrdersVO;

public class ProcessController extends MultiActionController {
	private ProcessService processService;
	
	public void setProcessService(ProcessService processService) {
		this.processService = processService;
	}

	/**
	 * 프로세스 메인
	 * work_order list를 가져온다.
	 */
	public ModelAndView main(HttpServletRequest request, HttpServletResponse response){
		System.out.println("process main...");
		ArrayList<HashMap> list = processService.getWorkOrderList();
		return new ModelAndView("process.process_main","list",list);
	}
	/**
	 * WO에 대한 주문수량 가져오기
	 */
	public ModelAndView getWoQty(HttpServletRequest request, HttpServletResponse response){
		HashMap map = new HashMap();
		String wO = request.getParameter("work_order_id");
		int qty = processService.getWoQty(wO);
		String start_no = processService.getStartNo(wO);
		map.put("qty", qty);
		map.put("start_no", start_no);
		//System.out.println("남아있는 주문량 : "+qty+"시작번호 : "+start_no);
		return new ModelAndView("JsonView","info",map);
	}
	/**
	 * operation가동시작
	 */
	public ModelAndView operation(HttpServletRequest request, HttpServletResponse response,WorkOrdersVO woVO){
		// 공정에 사용되는 재고 수량 빼기 
		int quantity=woVO.getQuantity();
		 this.processService.updateItemByQuantity(quantity);		  
		ArrayList<HashMap> info = new ArrayList<HashMap>();		
		MemberVO mem = (MemberVO)request.getSession().getAttribute("vo");
		String mem_id = mem.getMem_id();
		System.out.println(woVO);
		processService.operation(woVO, mem_id);		
		return new ModelAndView("JsonView","info","완료");
	}
	/**
	 * 실시간 업데이트
	 */
	public ModelAndView realTimeUpdate(HttpServletRequest request, HttpServletResponse response,WorkOrdersVO woVO){
		//System.out.println("실시간 업데이트");
		//System.out.println(woVO);
		MemberVO mem = (MemberVO)request.getSession().getAttribute("vo");
		String mem_id = mem.getMem_id();
		ArrayList<RealTimeInfoVO> info = processService.realTimeUpdate(woVO,mem_id);
		return new ModelAndView("JsonView","info",info);
	}
	/**
	 * 트래킹
	 */
	public ModelAndView trackingSN(HttpServletRequest request, HttpServletResponse response){
		String serial_no = request.getParameter("serial_no");
		HashMap map=new HashMap();		
		map.put("serial_no", serial_no);
		map.put("pageNo", 1);
		System.out.println(serial_no);
		ListVO lvo=processService.trackingSN(map);
		System.out.println(lvo);
		return new ModelAndView("main/process/tracking_result","lvo",lvo);
	}
	
}
